package com.qf.studentmanager.service;

import com.qf.studentmanager.config.DBConfig;
import com.qf.studentmanager.dao.TeacherDao;
import com.qf.studentmanager.model.RespPageBean;
import com.qf.studentmanager.model.Teacher;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class TeacherService {
    TeacherDao teacherDao = new TeacherDao();
    public RespPageBean getAllTeacherByPage(String page, String rows, String sort, String oder) {
        if (page == null){
            page = "1";
        }
        if (rows == null){
            rows = "10";
        }
        if (sort == null ){
            sort = "asc";
        }
        if (oder == null){
            oder = "tid";
        }

        int p = Integer.parseInt(page);
        int r = Integer.parseInt(rows);

        RespPageBean respPageBean = new RespPageBean();

        Long total = teacherDao.getTotal();
        respPageBean.setTotal(total);
        List<Teacher> teacherList = teacherDao.getTeahcerByPage((p-1)*r , r ,sort , oder);
        respPageBean.setRows(teacherList);
        return respPageBean;
    }

    public Integer addTeacher(String number, String name, String gender, String phone, String qq, String[] courses) {
      Teacher teacher = teacherDao.getTeacherByPhone(number);

      if (teacher != null ){
          return -2; //教师重复
      }else {
          Connection connection = DBConfig.getConnection();
          try {
              connection.setAutoCommit(false);
              Teacher teacher1 = new Teacher();
              teacher1.setNumber(number);
              teacher1.setName(name);
              teacher1.setGender(gender);
              teacher1.setPhone(phone);
              teacher1.setQq(qq);
              int r1 =  teacherDao.addTeacher(teacher1);

              int r2 = teacherDao.addTeacherAndCourse(teacher1 ,courses);
              connection.commit();
              return (r1 == 1 && r2 == courses.length) ? 1 : -1;
          } catch (SQLException e) {
              e.printStackTrace();
              try {
                  connection.rollback();
              } catch (SQLException ex) {
                  ex.printStackTrace();
              }
          }
      }

        return -1;
    }

    public Integer updateTeacherCourseByNumber(Teacher teacher, String[] course) {
        Connection connection = DBConfig.getConnection();
        try {
            connection.setAutoCommit(false);
            Integer r1 = teacherDao.updateTeacher(teacher);
            teacherDao.deleteTeacherCourse(teacher.getTid());
            Integer r2 = teacherDao.addTeacherAndCourse(teacher, course);
            connection.commit();

            if (r1 == 1 && r2 == course.length){
                return 1;
            }else {
                return -1;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return -1;
    }

    public Integer deleteTeacherByTid(String[] tids) {
        Connection connection = DBConfig.getConnection();


        try {
            connection.setAutoCommit(false);
            Integer r1 =teacherDao.deleteTeahcers(tids);
            Integer r2 = teacherDao.delTeacherAndCourses(tids);

            connection.commit();
            if (r1 == tids.length  ){
                return 1;
            }else {
                return -1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return -1;
    }
}
